package com.fare.taluo.application.impl;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.apache.hadoop.hbase.client.ResultScanner;
import org.foreveross.hbase.client.HBaseHelper;
import org.foreveross.hbase.client.HBasePlaneinfoHelper;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.fare.taluo.application.ChannelTariffsApplication;
import com.fare.taluo.core.domain.config.channel.SourceEntity;
import com.fare.taluo.fetl.domain.entity.OfficChannelSpreadEntity;
import com.fare.taluo.fetl.domain.entity.OfficialPlusDayEntity;
import com.fare.taluo.util.DateUtils;
import com.fare.taluo.vo.core.HomeChannelCompare;
@Transactional
@Scope("singleton")
@Service("channelTariffsApplication")

/**
 * 
 * @author xiaoshuwen
 *	渠道首页加载数据 业务实现层
 */
public class ChannelTariffsApplicationImpl implements ChannelTariffsApplication{

	/**
	 * 查询价差比排名前10航线信息
	 */
	
	@Override
	public List<HomeChannelCompare> getPlaneInfoByRowKey(Date flightDate,String sourceCode,String formCity,String toCity) {
		List<HomeChannelCompare> lists = new ArrayList<HomeChannelCompare>();
		try {
			//Date reportDate =DateUtils.strToDate(nowDate, "yyyyMMdd");
			OfficChannelSpreadEntity entity =new OfficChannelSpreadEntity();
			List<OfficChannelSpreadEntity> listEntity =entity.findByCondition(
					DateUtils.getCurrentDateStartTime(), 
					flightDate, formCity, toCity, null, null, sourceCode);
			if(null!=listEntity && listEntity.size()>0){
				for(OfficChannelSpreadEntity en :listEntity){
					if(null!=en){
						HomeChannelCompare vo = this.entityToHome(en);
						lists.add(vo);
					}
				}
			}
			
			/*StringBuffer startRowKey = new StringBuffer();
			Calendar cn7 = Calendar.getInstance();
			SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd");
			startRowKey.append(nowDate);
			startRowKey.append(sourceCode);
			if(StringUtils.isBlank(formCity)){
				formCity="000";
			}
		    startRowKey.append(formCity);
			
			if(StringUtils.isBlank(toCity)){
				toCity="000";
			}
			startRowKey.append(toCity);
			startRowKey.append("00000000");
			
			StringBuffer endRoweKey= new StringBuffer();
			endRoweKey.append(nowDate);
			endRoweKey.append(sourceCode);
			if(StringUtils.isBlank(formCity) || formCity.equalsIgnoreCase("000")){
				formCity="zzz";
			}
			endRoweKey.append(formCity);
			
			if(StringUtils.isBlank(toCity) || toCity.equalsIgnoreCase("000")){
				toCity="zzz";
			}
			endRoweKey.append(toCity);
			endRoweKey.append("zzzzzzzz");
			ResultScanner scan = new HBaseHelper().getRecords("LP_FLIGHT_OFFICIAL_CHANNEL_PLUS_TOP5", startRowKey.toString(), endRoweKey.toString());
			lists=new HBasePlaneinfoHelper().transformHBaseResultToPlaneinfosChannels(scan);*/
			Collections.sort(lists);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(lists.size()>=10){
			return lists.subList(0,10);
		}else{
			return lists.subList(0,lists.size());
		}
		
	}
/**
 * 查询区域航线信息(目前不用)
 */
	@Override
	public List<HomeChannelCompare> getPlaneInfoByRowKeySoce(String sid,
			String flydate, String fromcity, String toCity, String planeNo) {
		List<HomeChannelCompare> lists = new ArrayList<HomeChannelCompare>();
		try {
			StringBuffer startRowKey = new StringBuffer();
			//flydate="20130122";
			startRowKey.append(sid);
			startRowKey.append(flydate);
			//startRowKey.append("20005");
			if(StringUtils.isBlank(fromcity)){
				fromcity="000";
			}
		    startRowKey.append(fromcity);
			
			if(StringUtils.isBlank(toCity)){
				toCity="000";
			}
			startRowKey.append(toCity);
			startRowKey.append("00000000");
			
			StringBuffer endRoweKey= new StringBuffer();
			endRoweKey.append(sid);
			endRoweKey.append(flydate);
			//endRoweKey.append("20005");
			if(StringUtils.isBlank(fromcity) || fromcity.equalsIgnoreCase("000")){
				fromcity="zzz";
			}
			endRoweKey.append(fromcity);
			
			if(StringUtils.isBlank(toCity) || toCity.equalsIgnoreCase("000")){
				toCity="zzz";
			}
			endRoweKey.append(toCity);
			endRoweKey.append("zzzzzzzz");
//			System.out.println("QYstartRowKey:+++++++++"+startRowKey);
//			System.out.println("QYendRoweKey:++++++++++"+endRoweKey);
			ResultScanner scan = new HBaseHelper().getRecords("LP_FLIGHT_AREA", startRowKey.toString(), endRoweKey.toString());
			//HBaseHelper.printResult(scan);
			lists=new HBasePlaneinfoHelper().transformHBaseResultToPlaneinfosChannels(scan);
			Collections.sort(lists);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(lists.size()>=5){
			return lists.subList(0,5);
		}else{
			return lists.subList(0,lists.size());
		}
	}
/**
 * 查询代理商与官网价差比超过3%，6%的代理商数量
 */
	@Override
	public Map<String,Integer> getChannelPriceSpread(String seachDate, String soruceName,
			String channelName, String fromCode, String toCode)  {
		Map<String,Integer> map = new HashMap<String,Integer>();
		try {
			Date reportDate =DateUtils.strToDate(seachDate, "yyyyMMdd");
			List<SourceEntity> listsource = new SourceEntity().findSourceEntityListByWbid("0");
			if(null!=listsource){
				for(SourceEntity s : listsource){
					int count03=0;
					int count06=0;
					OfficialPlusDayEntity entity =new OfficialPlusDayEntity();
					List<OfficialPlusDayEntity> listEntity =entity.findByCondition(reportDate, fromCode, toCode, s.getHbaseKey(), soruceName);
					if(null!=listEntity && listEntity.size()>0){
						for(OfficialPlusDayEntity en :listEntity){
							if(null!=en.getThreshold03()){
								count03+=en.getThreshold03();
							}
							if(null!=en.getThreshold06()){
								count06+=en.getThreshold06();
							}
						}
					}
					/*StringBuffer startRowKey = new StringBuffer();
					startRowKey.append(seachDate);
					startRowKey.append(soruceName);
					startRowKey.append(s.getHbaseKey());
					if(StringUtils.isBlank(fromCode) && StringUtils.isBlank(toCode)){
						startRowKey.append("000");
						startRowKey.append("000");
					}else{
						startRowKey.append(fromCode);
						startRowKey.append(toCode);
					}
					
					StringBuffer endRoweKey = new StringBuffer();
					endRoweKey.append(seachDate);
					endRoweKey.append(soruceName);
					endRoweKey.append(s.getHbaseKey());
					if(StringUtils.isBlank(fromCode) && StringUtils.isBlank(toCode)){
						endRoweKey.append("zzz");
						endRoweKey.append("zzz");
					}else{
						endRoweKey.append(fromCode);
						endRoweKey.append(toCode);
					}
					ResultScanner scan = HBaseHelper.getRecords("LP_FLIGHT_OFFICIAL_CHANNEL_PLUS_DAY", startRowKey.toString(), endRoweKey.toString());
//					System.out.println("++"+startRowKey.toString());
					
					Map<String,Integer> mapl = HBasePlaneinfoHelper.getSpreadsthanBySeRowKey(scan,endRoweKey.toString());
//					System.out.println("++"+startRowKey.toString());
					for(String key : mapl.keySet()){
						if(key.substring(key.length()-2, key.length()).equalsIgnoreCase("03")){
							count03+=mapl.get(key);
						}
						if(key.substring(key.length()-2, key.length()).equalsIgnoreCase("06")){
							count06+=mapl.get(key);
						}
					}*/
					map.put("tq"+s.getHbaseKey()+"03", count03);
					map.put("tq"+s.getHbaseKey()+"06", count06);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} 
		return map;
	}
/**
 * 查询过去7天(含当天)排名前三的价差比
 */
	@Override
	public List<HomeChannelCompare> getPlaneInfoByRowKeySeven(String nowDate,
			String pastDate, String sourceCode, String formCity, String toCity) {
		List<HomeChannelCompare> lists = new ArrayList<HomeChannelCompare>();
		try {
			OfficChannelSpreadEntity entity =new OfficChannelSpreadEntity();
			
			Date startDay =DateUtils.strToDate(pastDate, "yyyyMMdd");
			List<OfficChannelSpreadEntity> listEntity =entity.findByCondition(startDay, null, formCity, toCity, null, null, sourceCode);
			if(null!=listEntity && listEntity.size()>0){
				for(OfficChannelSpreadEntity en :listEntity){
					if(null!=en){
						HomeChannelCompare vo = this.entityToHome(en);
						lists.add(vo);
					}
				}
			}
			
			/*StringBuffer startRowKey = new StringBuffer();
			startRowKey.append(pastDate);
			startRowKey.append(sourceCode);
			if(StringUtils.isBlank(formCity)){
				formCity="000";
			}
		    startRowKey.append(formCity);
			
			if(StringUtils.isBlank(toCity)){
				toCity="000";
			}
			startRowKey.append(toCity);
			startRowKey.append("00000000");
			
			StringBuffer endRoweKey= new StringBuffer();
			endRoweKey.append(nowDate);
			endRoweKey.append(sourceCode);
			if(StringUtils.isBlank(formCity) || formCity.equalsIgnoreCase("000")){
				formCity="zzz";
			}
			endRoweKey.append(formCity);
			
			if(StringUtils.isBlank(toCity) || toCity.equalsIgnoreCase("000")){
				toCity="zzz";
			}
			endRoweKey.append(toCity);
			endRoweKey.append("zzzzzzzz");*/
			
			//新查询组件优化(暂时不用优化)
//			List<HBaseFilterVO> vos = new ArrayList<HBaseFilterVO>();
//			vos.add(new HBaseFilterVO("planeinfo", "lowerPrice"));
//			vos.add(new HBaseFilterVO("planeinfo", "carrierName"));
//			vos.add(new HBaseFilterVO("planeinfo", "flightId"));
//			vos.add(new HBaseFilterVO("planeinfo", "flightBatchDate"));
//			vos.add(new HBaseFilterVO("planeinfo", "flightBatchStartTime"));
//			vos.add(new HBaseFilterVO("planeinfo", "flightBatchEndTime"));
//			vos.add(new HBaseFilterVO("planeinfo", "grabChannel"));
//			vos.add(new HBaseFilterVO("planeinfo", "grabDate"));
//			List<PlaneInfo> list=HbaseCustom.findByConditionsToPlaneInfo("LP_FLIGHT_OFFICIAL_CHANNEL_PLUS_TOP5", startRowKey.toString(), endRoweKey.toString(), vos, null);
//			
			/*ResultScanner scan = new HBaseHelper().getRecords("LP_FLIGHT_OFFICIAL_CHANNEL_PLUS_TOP5", startRowKey.toString(), endRoweKey.toString());
			lists=new HBasePlaneinfoHelper().transformHBaseResultToPlaneinfosChannels(scan);*/
			
			Collections.sort(lists);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(lists.size()>=3){
			return lists.subList(0,3);
		}else{
			return lists.subList(0,lists.size());
		}
	}
	
	public HomeChannelCompare entityToHome(OfficChannelSpreadEntity en){
		HomeChannelCompare vo =new HomeChannelCompare();
		vo.setAgentName(en.getChannelName());
		vo.setChannelPrice(en.getChannelPrice().toString());
		vo.setFlightBatch(en.getFlightNo());
		vo.setFlightBatchDate(DateUtils.dateToStr(en.getFlightDate(), "yyyy-MM-dd"));
		vo.setGrabChannelNum(en.getHbaseKey());//??
		vo.setGrabDate(DateUtils.dateToStr(en.getGrabTime(), "yyyyMMddHHmmssSS"));
		vo.setHomePrice(en.getHomePrice().toString());
		vo.setGrabChannel(en.getHbaseName());
		return vo;
	}
}
